//
//  readingAndOrderViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "readingAndOrderViewController.h"
#import "ReadingViewController.h"
#import "OrdeingViewController.h"
#import "RecomOrderViewController.h"

@interface readingAndOrderViewController ()

@property (nonatomic, strong) ReadingViewController *readVC;
@property (nonatomic, strong) OrdeingViewController *orderVC;
@property (nonatomic, strong) UISegmentedControl *segmentC;

@end

@implementation readingAndOrderViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"hideView" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"showView" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-add"] style:(UIBarButtonItemStylePlain) target:self action:@selector(addOrderAction:)];
    
    self.readVC = [[ReadingViewController alloc] init];
    [self addChildViewController:self.readVC];
    [self.view addSubview:self.readVC.view];
    
    self.orderVC = [[OrdeingViewController alloc] init];
    [self addChildViewController:self.orderVC];
    [self.view addSubview:self.orderVC.view];

    [self.view bringSubviewToFront:self.readVC.view];
    NSArray *arr = @[@"推荐阅读", @"我的订阅"];
    self.segmentC = [[UISegmentedControl alloc] initWithItems:arr];
    self.segmentC.tintColor = [UIColor whiteColor];
    self.segmentC.selectedSegmentIndex = 0;
    [self.segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:(UIControlEventValueChanged)];
    self.segmentC.frame = CGRectMake(SCREEN_WIDTH / 2 - 100, 15, 200, 45);
    [self.navigationController.view addSubview:self.segmentC];
    
#pragma 接受隐藏segment消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidenSegment) name:@"hideView" object:nil];
#pragma 接受现实segment消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSegment) name:@"showView" object:nil];

}

- (void)hidenSegment
{
    self.segmentC.hidden = YES;
}

- (void)showSegment
{
    self.segmentC.hidden = NO;
    [self.navigationController.view bringSubviewToFront:self.segmentC];
}

- (void)addOrderAction:(UINavigationItem *)sender
{
    RecomOrderViewController *recomdOrderVC = [[RecomOrderViewController alloc] init];
    self.segmentC.hidden = YES;
    [self.navigationController pushViewController:recomdOrderVC animated:YES];
}

- (void)segmentAction:(UISegmentedControl *)sender
{
    if (0 == sender.selectedSegmentIndex) {
        [self.view bringSubviewToFront:self.readVC.view];
    }
    if (1 == sender.selectedSegmentIndex) {
        [self.view bringSubviewToFront:self.orderVC.view];
    }
    [self.navigationController.view bringSubviewToFront:sender];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
